﻿using Archives.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Archives.DbHelper
{
    public abstract class SqlPaging
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageSize">每页显示多少条数据</param>
        /// <param name="pageNumber">页数</param>
        /// <param name="sortColumn"></param>
        /// <param name="sortDirection"></param>
        /// <returns></returns>
        public static TotalInfo SqlPaing(int? pageSize,int? pageNumber,string sortColumn, string sortDirection,string sql)
        {
            //总记录数
            var totalRecouds = SqlHelper.GetAllBookCount(sql);
            //总页数
            var totalPages = totalRecouds % pageSize == 0 ? totalRecouds / pageSize : totalRecouds / pageSize + 1;
            return new TotalInfo
            {
                TotalPages = totalPages,
                TotalRecouds = totalRecouds
            };
        }
        /// <summary>
        /// 获取结果集
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <param name="sortColumn"></param>
        /// <param name="sortDirection"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable TableInfo(int? pageSize, int? pageNumber, string sortColumn, string sortDirection, string sql)
        {
            //开始位置
            var startPage = pageSize * (pageNumber - 1) + 1;
            //结束位置
            var endPage = startPage + pageSize;
            var sqlPage = string.Format(@"select a.* from ({0}) a where a.number >= {1} and a.number < {2}", sql, startPage, endPage);
            //结果集
            var data = SqlHelper.ExcuteDataQuery(sqlPage);
            return data;
        }

    }
}